package com.oweight.sagacious.auth.server.infrastructure.interceptor;

import com.oweight.sagacious.auth.server.domain.service.IClientServiceManager;
import com.oweight.sagacious.auth.server.infrastructure.configuration.ClientConfiguration;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 设置Feign Token
 *
 * @author 周广明
 * @version v1 2019/10/21 16:10
 */
@Slf4j
public class ClientTokenInterceptor implements RequestInterceptor {

    @Autowired
    private ClientConfiguration clientConfiguration;

    @Autowired
    private IClientServiceManager authClientService;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        try {
            requestTemplate.header(clientConfiguration.getClientTokenHeader(), authClientService.apply(clientConfiguration.getClientId(), clientConfiguration.getClientSecret()));
        } catch (Exception e) {
            log.error("设置Feign Token错误！", e);
        }
    }
}
